//
//  MainTabBarViewController.m
//  ZPFHimalayan
//
//  Created by zhou on 2018/11/23.
//  Copyright © 2018 MissZhou. All rights reserved.
//

#import "MainTabBarViewController.h"
#import "ZPFTabBar.h"
#import "ZPFHomeViewController.h"
#import "ZPFListenViewController.h"
#import "ZPFPlayViewController.h"
#import "ZPFFoundViewController.h"
#import "ZPFMineViewController.h"
#import "RootNavigationViewController.h"
#import "UITabBar+CustomBadge.h"

@interface MainTabBarViewController ()<UITabBarControllerDelegate>
@property (nonatomic, strong) NSMutableArray *vcs;
@end

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    //初始化Tabbar
    [self setUpTabbar];
    //添加子视图控制器
    [self setUpAllChildViewController];
}

#pragma mark - 初始化Tabbar
- (void)setUpTabbar{
    [self setValue:[ZPFTabBar new] forKey:@"tabBar"];
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    [self.tabBar setBackgroundImage:[UIImage new]];
    //通过这两个参数来调整badge位置
    //    [self.tabBar setTabIconWidth:29];
    //    [self.tabBar setBadgeTop:9];
}

#pragma mark - 初始化Vc
- (void)setUpAllChildViewController{
    self.vcs = @[].mutableCopy;
    //首页
    ZPFHomeViewController *homeVC = [[ZPFHomeViewController alloc]init];
    [self setUpChildViewController:homeVC title:@"首页" imageName:@"icon_tabbar_homepage" selectImageName:@"erha"];//icon_tabbar_homepage_selected
    
    //我听
    ZPFListenViewController *listenVC = [[ZPFListenViewController alloc] init];
    [self setUpChildViewController:listenVC title:@"我听" imageName:@"icon_tabbar_onsite" selectImageName:@"icon_tabbar_onsite_selected"];
    
    //正在播放
    ZPFPlayViewController *playVC = [[ZPFPlayViewController alloc] init];
    [self setUpChildViewController:playVC title:@"正在播放" imageName:@"icon_tabbar_merchant_normal" selectImageName:@"erha"];//icon_tabbar_merchant_selected
    
    //发现
    ZPFFoundViewController *foundVC = [[ZPFFoundViewController alloc] init];
    [self setUpChildViewController:foundVC title:@"发现" imageName:@"icon_tabbar_onsite" selectImageName:@"icon_tabbar_onsite_selected"];
    
    //我的
    ZPFMineViewController *mineVC = [[ZPFMineViewController alloc] init];
    [self setUpChildViewController:mineVC title:@"我的" imageName:@"icon_tabbar_mine" selectImageName:@"icon_tabbar_mine_selected"];
    
    self.viewControllers = _vcs;
}

- (void)setUpChildViewController:(UIViewController *)viewController title:(NSString *)title imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName{
    viewController.title = title;
    viewController.tabBarItem.title = title;//跟上面一样的效果
    viewController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [[UIImage animatedImageNamed:selectImageName duration:3] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //未选中字体颜色
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:SYSTEMFONT(10.0f)} forState:UIControlStateNormal];
    //选中的字体颜色
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CNavigationBackGroundColor} forState:UIControlStateSelected];
    //包装导航控制器
    RootNavigationViewController *nav = [[RootNavigationViewController alloc] initWithRootViewController:viewController];
    [self.vcs addObject:nav];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSLog(@"选中：%lu",(unsigned long)tabBarController.selectedIndex);
}

-(void)setRedDotWithIndex:(NSInteger)index isShow:(BOOL)isShow{
    if (isShow) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleRedDot value:0 atIndex:index];
    }else{
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:0 atIndex:index];
    }
    
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}
@end
